//
//  MXServiceController.m
//  HNBaseProject
//
//  Created by xujie on 2024/6/4.
//

#import "MXServiceController.h"
#import "MxPublicNav.h"

@interface MXServiceController () <MxNavigationViewDelegate>
@property (nonatomic, strong) MxPublicNav *nav;
@property (nonatomic, strong) YYLabel *titleLabel;
@end

@implementation MXServiceController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
    self.nav = [[MxPublicNav alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kNavBarHeight)];
    self.nav.titleLab.text = @"喵享家客服";
    self.nav.delegate = self;
    [self.view addSubview:self.nav];
    [self getPhoneNum];
}

- (void)getPhoneNum {
    MJWeakSelf
    [self.view makeToastActivity];
    [[DCHttpClient shareClient] requestWithPath:@"api/app/appConfig/getAppConfigInfo" params:@{} httpMethod:DCHttpRequestGet sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        [weakSelf.view hideToastActivity];
        NSDictionary * dict = [responseObject mj_JSONObject];
        if (dict[DC_ResultCode_Key] && [dict[DC_ResultCode_Key] integerValue] == DC_Result_Success) {
            NSString *phone = dict[@"data"][@"customerPhone"];
            [weakSelf createUIWithPhone:phone];
        } else {

            [weakSelf.view xjmakeToast:dict[DC_ResultMsg_Key]];
        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [weakSelf.view hideToastActivity];
    }];
}

- (void)createUIWithPhone:(NSString *)phone {
    
    MJWeakSelf
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(15, kNavBarHeight+10, kScreenW-30, 200)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 15;
    [self.view addSubview:bgView];
    bgView.userInteractionEnabled = YES;
    
    NSString *str = [NSString stringWithFormat:@"请拨打: %@ 联系客服",phone];//@"";
    NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:str];
    titleText.font = PFRFontMedium(18);
    titleText.color = [UIColor dc_colorWithHexString:@"#0F0F0F" alpha:0.5];
    NSRange range = [str rangeOfString:[NSString stringWithFormat:@"请拨打: %@",phone]];

    [titleText setTextHighlightRange:range color:[UIColor colorWithHexString:@"#0F0F0F"] backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [weakSelf phoneCallWithPhone:phone];
    }];
    
    YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(kScreenW-30, MAXFLOAT)];;
    YYTextLayout *titleTextLayout = [YYTextLayout layoutWithContainer:container text:titleText];
    YYLabel *titleLabel = [[YYLabel alloc] init];
    [self.view addSubview:titleLabel];
    self.titleLabel = titleLabel;
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(bgView);
    }];
    titleLabel.textLayout = titleTextLayout;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
}

-(void)leftTap {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)phoneCallWithPhone:(NSString *)phone {
    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", phone]];
    UIApplication *application = [UIApplication sharedApplication];
    if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
        if (@available(iOS 10.0, *)) {
            [application openURL:URL options:@{}
               completionHandler:nil];
        } else {
            
        }
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self dc_navBarHidden:YES];
}

@end
